package controller;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import interceptor.LoginCheck;

import java.util.List;

/**
 * Created by XingYang on 2018/6/24.
 */
@Before(LoginCheck.class)
public class Maincontroller extends Controller {
    @ActionKey("/showCategory")
    public void showcategory(){
        List<Record> category = Db.find("select * from category");
        setAttr("category",category);
        renderFreeMarker("/template/category.ftl");
    }
    @ActionKey("/findCategory")
    public void findCategory(){
        int id = getParaToInt("c_id");
        List<Record> category = Db.find("select * from category where id="+id);
        setAttr("category",category);
        renderFreeMarker("/template/modify.ftl");
    }
    @ActionKey("/delCategory")
    public void delCategory(){
        int id = getParaToInt("c_id");
        Db.update("UPDATE category SET deleted=1 WHERE id ="+id);
        redirect("/showCategory");
    }
    @ActionKey("/enableCategory")
    public void enableCategory(){
        int id = getParaToInt("c_id");
        Db.update("UPDATE category SET deleted=0 WHERE id ="+id);
        redirect("/showCategory");
    }
    @ActionKey("/modifyCategory")
    public void modifyCategory(){
        int id = getParaToInt("c_id");
        String name = getPara("name");
        if(name!=null)
        Db.update("UPDATE category SET name='"+name+"' WHERE id ="+id);
        redirect("/showCategory");
    }
    @ActionKey("/addCategory")
    public void addCategory(){
        String name = getPara("name");
        if(name!=null)
        Db.update("INSERT category values (null,'"+name+"',0)");
        redirect("/showCategory");
    }
    @ActionKey("/add")
    public void addc(){
        renderFreeMarker("/template/addcategory.ftl");
    }
}
